package com.mynetwork.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.StatFs;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;


public class SystemInfoUtils {
	private static final String TAG = "SystemInfoUtils";

	public static void hideKeyboard(View view) {
		InputMethodManager imm = (InputMethodManager)view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
	}
	
	public static void showKeyboard(View view) {
		InputMethodManager imm = (InputMethodManager)view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.showSoftInput(view, 0);
	}
	
	public static int getConnectedNetwork(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		
		NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (wifiNetwork != null && wifiNetwork.isConnectedOrConnecting())
			return ConnectivityManager.TYPE_WIFI;
		else if (mobileNetwork != null && mobileNetwork.isConnectedOrConnecting())
			return ConnectivityManager.TYPE_MOBILE;
		else 
			return -1;
	}
	
	public static int getAppVersionCode(Context context) {
		try {
		   PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
		   return pi.versionCode;
		} catch(Exception e) {
			Log.e(TAG, "" + e);
		}
		return -1;
	}

	public static String getAppVersion(Context context) {
		try {
		   PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
		   return pi.versionName;
		} catch(Exception e) {
			Log.e(TAG, "" + e);
		}
		return "";
	}
	
	public static String getDeviceId(Context context) {
		TelephonyManager tManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		return tManager.getDeviceId();
	}
	
	public static int getSimState(Context context) {
		TelephonyManager tManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		return tManager.getSimState();
	}
	
	public static String getMCC(Context context) {
		TelephonyManager tManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		return tManager.getNetworkCountryIso();
	}
	
	public static String getMCCMNC(Context context) {
		TelephonyManager tManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		return tManager.getNetworkOperator();
	}
	
	public static boolean isPortrait(Context context) {
		DisplayMetrics displayMetrics = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		int deviceWidth = displayMetrics.widthPixels;
		int deviceHeight = displayMetrics.heightPixels;
		if(deviceHeight > deviceWidth) {
			return true;
		} else {
			return false;
		}
	}
	
	public static boolean isDevicePhone(Context context) {
		if (getRealScreenInch(context) < 7)
			return true;
		return false;
	}
	public static double getRealScreenInch(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
        double x = Math.pow(dm.widthPixels/dm.xdpi,2);
        double y = Math.pow(dm.heightPixels/dm.ydpi,2);
        return Math.sqrt(x+y);
	}
}
